// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef UDP_TRANS_NET_TRANS_DEF_H
#define UDP_TRANS_NET_TRANS_DEF_H

#include <string>

enum NET_TRANS_PEER_TYPE {
    PEER_CLIENT,
    PEER_SERVER
};

struct TransConfigParam {
    uint32_t curBandwidth;  // 初始通道带宽, kbps
    uint32_t maxBandwidth;  // 最大通道带宽, kbps
    uint32_t minVideoSendBitrate;   // 视频流发送最小码率，kbps
    uint32_t maxVideoSendBitrate;   // 视频流发送最大码率，kbps
};

enum NET_TRANS_LOG_LEVLE {
    LOG_LEVEL_OFF,    // 关闭日志
    LOG_LEVEL_ERROR,  // error级别
    LOG_LEVEL_WARN,   // warn级别
    LOG_LEVEL_INFO,   // info级别
    LOG_LEVEL_DEBUG   // debug级别
};

struct AudioJbDecode {
    int32_t bfi;
    uint8_t* payload;
    uint32_t inputLength;
    int16_t* outputData;
    uint32_t outputLength;
    uint32_t outputBufSize;
    int32_t frameType;
    uint32_t metaDataOffset;
};

struct VideoRecvStats {
    uint32_t recvFrameRate;         // 接收帧率 秒级统计
    uint32_t redRate;               // 冗余率，秒级统计 冗余包数/总包数
    uint32_t recoverRate;           // 恢复率，秒级统计，恢复的包数/总包数
    uint32_t jbListpacketNum;       // JB包链表包数量(注: 包连续性出包策略下有值)
    uint32_t sendKeyRequestCnt;     // I帧请求数量
    int32_t jbJitter;               // JB评估抖动值(包抖动和帧抖动两方面影响)
    uint32_t refFrameErrorCnt;      // 参考帧关系解析异常总次数
    uint64_t aveJbDelayAfterBuild;  // JB平均出帧时延(出帧时间 – 组帧完整时的平均时间) 单位ms
    uint64_t recvAveDelay;  // 出帧跟尾包平均时延 单位us
    uint64_t recvMaxDelay; // 出帧跟尾包最大时延 单位us
};

/**
 * @brief 音频接收流CHR统计信息
 * @direction:Output
 */
struct AudioRecvStats {
    uint32_t receiveTotalTime;      // 收包持续时间
    uint32_t plcCount;              // 音频plc算法调用总次数
    uint32_t tsmCompressCount;      // TSM压缩总次数
    uint32_t tsmStretchCount;       // TSM拉伸总次数
    int32_t totalDelay;             // 当前估算的总时延
    int32_t estTotalDelay;          // 当前音频目标时延
    uint64_t noVoiceFrameCnt;       // 音频静音帧总数
    uint32_t getFrameCount;         // 音频出帧总次数(外部调用)
};

struct StreamRecvStats {
    uint32_t recvBitrate;               // 接收码率
    uint32_t lostRate;                  // 丢包率[0,100]
    uint32_t jitter;                    // 抖动值
    uint32_t lostPktCnt;                // 累计丢包数
    uint32_t maxContLostPktCnt;         // (查询周期内)最大连续丢包数
    uint32_t recvTotalFrameCnt;         // 下行接收总帧数
    uint32_t jbDepth;                   // JB缓存深度
    uint32_t jbTotalFrameCnt;           // JB输出总帧数
    uint32_t packetRate;                // 接收包率
    uint32_t periodNotRecvPktCnt;       // (查询周期内)累计收包间隔超200ms次数
    uint32_t periodNotRecvPktTime;      // (查询周期内)累计收包间隔超200ms时长
    uint32_t periodFreezeFrameCnt;      // (查询周期内)出帧间隔大于200ms的总帧数
    uint64_t periodFreezeTime;          // (查询周期内)出帧间隔大于200ms的间隔累积和
    uint32_t sendNackRequestCnt;        // 发送nack请求总数量
    union {
        VideoRecvStats videoStats;  // 视频CHR统计信息
        AudioRecvStats audioStats;  // 音频CHR统计信息
    };
};

struct VideoSendStats {
    uint32_t sendFrameRate;        // 实际发送帧率
    uint32_t availableEncBitrate;  // 有效编码带宽
    uint32_t inputFrameCnt;        // 上行累计编码输入帧数量
    uint32_t sendFrameCnt;         // 上行实际发送帧数量
    uint32_t redRate;              // 冗余率
    uint32_t recvKeyRequestCnt;    // 收到I帧请求总数量
    uint32_t recvNackRequestCnt;   // 收到nack帧请求总数量
    uint32_t nackResponsePktCnt;   // nack响应的包数量
    uint32_t nackNotRspCnt;        // nack不响应包数量
};

struct StreamSendStats {
    uint32_t encBitrate;                // 流级实际编码发送码率
    uint32_t fecBitrate;                // 流级实际fec发送码率
    uint32_t arqBitrate;                // 流级实际arq发送码率
    uint32_t packetRate;                // 发送包率，主要用于包头扣减
    uint32_t rtt;                       // RTT时延
    uint32_t remoteJitter;              // 对端反馈的接收jitter
    uint32_t remoteLostRate;            // 对端反馈的接收丢包率[0,100]
    uint64_t pacingSendMaxDelay;        // 流控发送单帧的最大时延，秒级统计     单位us
    uint64_t pacingSendAveDelay;        // 流控发送单帧的平均时延，秒级统计     单位us
    uint32_t periodNotSendPktCnt;       // (查询周期内)累计发包间隔超200ms次数
    uint64_t periodNotSendPktTime;      // (查询周期内)累计发包间隔超200ms时长  单位us
    VideoSendStats videoStats;    // 视频CHR统计信息
};

struct TouchEventParam {
    uint8_t id;
    uint8_t action;
    uint16_t x;
    uint16_t y;
    uint16_t pressure;
    int32_t time;
    uint8_t orientation;
    uint16_t height;
    uint16_t width;
} __attribute__((packed));

struct KeyEventParam {
    uint16_t keycode;
    uint16_t action;
} __attribute__((packed));

struct MotionEventParam {
    uint16_t masterAxis;
    int32_t masterValue;
    uint16_t secondaryAxis;
    int32_t secondaryValue;
} __attribute__((packed));

typedef int32_t (*RecvVideoDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvAudioDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvAudioDecodeCallback)(int32_t streamId, AudioJbDecode* audioJbDecode);
typedef int32_t (*RecvCmdDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvSensorDataDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvLocationDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvTouchEventDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvKeyEventDataCallback)(uint8_t* data, uint32_t length);
typedef int32_t (*RecvMotionEventDataCallback)(uint8_t* data, uint32_t length);
typedef void (*UpdateBitrateCallback)(uint32_t encBitrate, uint32_t totalBitrate);
typedef void (*RequestKeyFrameCallback)();
typedef void (*TransLogCallback)(const char* str, uint32_t length);
typedef void (*RemoteAddressChangedCallBack)(std::string ip, uint16_t port);
typedef bool (*IsVerifiedIpCallBack)(std::string ip);

#endif //UDP_TRANS_NET_TRANS_DEF_H
